//
// Created by 33145 on 2023/1/29.
//

#ifndef SYLAR_FD_MANAGER_H
#define SYLAR_FD_MANAGER_H

#include <memory>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include "thread.h"
#include "iomanager.h"
#include "singleton.h"

namespace sylar {
class FdCtx : public std::enable_shared_from_this<FdCtx> {
public:
  typedef std::shared_ptr<FdCtx> ptr;
  FdCtx(int fd);
  ~FdCtx();

  bool init();
  bool isInit() const { return m_isInit; }
  bool isSocket() const { return m_isSocket; }
  bool isClose() const { return m_isClosed; }

  void setUserNonblock(bool v) { m_userNonblock = v; }
  bool getUserNonblock() const { return m_userNonblock; }

  void setSysNonblock(bool v) { m_sysNonblock = v; }
  bool getSysNonblock() const { return m_sysNonblock; }

  void setTimeout(int type, uint64_t v);
  uint64_t getTimeout(int type);
private:
  /// 是否初始化
  bool m_isInit: 1;
  /// 是否socket
  bool m_isSocket: 1;
  /// 是否hook非阻塞
  bool m_sysNonblock: 1;
  /// 是否用户主动设置非阻塞
  bool m_userNonblock: 1;
  /// 是否关闭
  bool m_isClosed: 1;
  /// 文件句柄
  int m_fd;
  /// 读超时时间毫秒
  uint64_t m_recvTimeout;
  /// 写超时时间毫秒
  uint64_t m_sendTimeout;

};

class FdManager {
public:
  typedef RWMutex RWMutexType;
  FdManager();

  FdCtx::ptr get(int fd, bool auto_create = false);
  void del(int fd);

private:
  RWMutexType m_mutex;
  std::vector<FdCtx::ptr> m_datas;
};

typedef Singleton<FdManager> FdMgr;
}

#endif //SYLAR_FD_MANAGER_H
